package com.ghc.action;

import java.util.ArrayList;
import com.ghc.dao.UserDao;
import com.ghc.pojo.User;
import com.ghc.util.MD5Util;
import com.opensymphony.xwork2.ActionContext;
public class UserAction {
	
	private User user = new User();
	private UserDao userDao;
	private String result;
	private ArrayList<User> htmlMgrList = new ArrayList<User>();
	private ArrayList<User> txtMgrList = new ArrayList<User>();
	
	public String addUser(){
		user.setPassword(MD5Util.string2MD5(user.getPassword()));
		try {
			userDao.addUser(user);
			result = "added";
		} catch (Exception e) {
			result = "fail";
		}
		return "success";
	}
	
	public String findUser(){
		int rs = userDao.findUser(user.getUsername());
		if(rs==0){
			result = "useful";
		}else{
			result = "exist";
		}
		return "success";
	}
	
	public String findMgr(){
		htmlMgrList = userDao.findHtmlMgr();
		txtMgrList = userDao.findTxtMgr();
		return "success";
	}
	
	public String delUser(){
		try {
			userDao.delUser(user.getId());
			result = "del";
		} catch (Exception e) {
			result = "fail";
		}
		return "success";
	}
	
	public String Login(){
		user.setPassword(MD5Util.string2MD5(user.getPassword()));
		int rs = userDao.Login(user);
		if(rs!=0){
			result = "pass";
		}else{
			result = "fail";
		}
		return "success";
	}
	
	public String ShowByRole(){
		user = userDao.findUserByName(user.getUsername());
		ActionContext.getContext().getSession().put("user",user);
		return "success";
	}
	
	public String changePW(){
		user.setPassword(MD5Util.string2MD5(user.getPassword()));
		try {
			userDao.changePW(user);
			result = "changed";
		} catch (Exception e) {
			result = "fail";
		}
		return "success";
	}
	
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public UserDao getUserDao() {
		return userDao;
	}
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}

	public ArrayList<User> getHtmlMgrList() {
		return htmlMgrList;
	}

	public void setHtmlMgrList(ArrayList<User> htmlMgrList) {
		this.htmlMgrList = htmlMgrList;
	}

	public ArrayList<User> getTxtMgrList() {
		return txtMgrList;
	}

	public void setTxtMgrList(ArrayList<User> txtMgrList) {
		this.txtMgrList = txtMgrList;
	}
}
